package com.example.homework;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

@WebServlet("/register")
public class Register extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get-----------------------");
        String paramsStr = req.getQueryString();
//        System.out.println(paramsStr);
        String[] paramsArr = paramsStr.split("&");

        String username = paramsArr[0].split("=")[1];
        String password = paramsArr[1].split("=")[1];
        String gender = paramsArr[2].split("=")[1];
        String hobby = paramsArr[3].split("=")[1];

        System.out.println("用户名" + username);
        System.out.println("密码" + password);
        System.out.println("性别" + gender);
        System.out.println("爱好" + hobby);


//        for (int i = 0; i < paramsStr.length() ; i++){
//            System.out.println(paramsArr[i]);
//            String[] paramArr = paramsArr[i].split("=");
//            for (int j = 0; j < paramArr.length; j++) {
//                System.out.println(paramArr[j]);
//            }
//        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        System.out.println("post--------------");
        /*BufferedReader reader = req.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
            String[] paramsArr = line.split("&");

            String username = paramsArr[0].split("=")[1];
            String password = paramsArr[1].split("=")[1];
            String gender = paramsArr[2].split("=")[1];
            String hobby = paramsArr[3].split("=")[1];

            System.out.println("用户名" + username);
            System.out.println("密码" + password);
            System.out.println("性别" + gender);
            System.out.println("爱好" + hobby);
        }*/

//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
//        String gender = req.getParameter("gender");
//        String hobby = req.getParameter("hobby");
//
//        System.out.println(username);
//        System.out.println(password);
//        System.out.println(gender);
//        System.out.println(hobby);

//        Enumeration<String> paramNames = req.getParameterNames();
//        while (paramNames.hasMoreElements()){
//            String name = paramNames.nextElement();
//            System.out.println(name + "==" + Arrays.toString(req.getParameterValues(name)));
//        }

        Map<String, String[]> paramMap = req.getParameterMap();
        Set<String> keySet = paramMap.keySet();
        for (String key : keySet) {
            System.out.println(key + "==" + Arrays.toString(paramMap.get(key)));
        }
    }
}
